<template>
  <div class="shopcart">
    <div class="content">
      <div class="content-left" @click="toogleList">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight':totalCount>0}">
            <i class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></i>
          </div>
          <div class="num" v-show="totalCount > 0" >{{totalCount}}</div>
        </div>
        <div class="price" :class="{'highlight':totalPrice>0}">
          ￥{{totalPrice}}
        </div>
        <div class="desc">另需配送费{{deliveryPrice}}元</div>
      </div>
      <div class="content-right">
        <div class="pay" :class="payClass">
          {{payDesc}}
        </div>
      </div>
    </div>
    <div class="ball-container">
      <transition-group name="drop"
        v-on:before-enter="ballBeforeEnter" v-on:enter="ballEnter" v-on:after-enter="ballAfterEnter">
        <div v-for="(ball, index) in balls" class="ball" v-show="ball.show" v-bind:key="index" :style="{top:ball.top+'px',left:ball.left+'px'}">
        </div>
      </transition-group>
    </div>
    <transition name="fold">
      <div class="shopcart-list" v-show="listShow">
        <div class="list-header">
          <h1 class="title">购物车</h1>
          <span class="empty" @click="clearSelectFoods">清空</span>
        </div>
        <div class="list-content" ref="listcontent">
          <ul>
            <li class="food border-1px" v-for="(food, index) in selectFoods" v-bind:key="index">
              <span class="name">{{food.name}}</span>
              <div class="price">
                <span>￥{{food.price}}</span>
              </div>
              <div class="cartcontrol-wrapper">
                <cartcontrol :food="food"></cartcontrol>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </transition>
  </div>
</template>

<script type="text/ecmascript-6">
import Vue from 'vue'
import Velocity from 'velocity-animate'
import BScroll from 'better-scroll'
import cartcontrol from '../../components/cartcontrol/cartcontrol'

export default {
  name: 'shopcart',
  components: { cartcontrol },
  props: {
    selectFoods: {
      type: Array,
      default() {
        return []
      },
    },
    deliveryPrice: {
      type: Number,
      default: 0,
    },
    minPrice: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      balls: [],
      dropBalls: [],
      fold: false,
    }
  },
  methods: {
    dropBall(elTarget) {
      const rect = elTarget.getBoundingClientRect()
      // if (this.balls.length > 0) return
      this.balls.push({
        show: true,
        left: rect.left,
        top: rect.top,
      })
    },
    ballBeforeEnter(el) {
      this.$nextTick(() => {
        Velocity(el, {
          opacity: 1,
          left: el.style.left,
          top: el.style.top,
        }, {
          duration: 0,
        })
      })
    },
    ballEnter(el, done) {
      /* eslint-disable */
      const endTop = window.innerHeight - 22 - 8
      const endLeft = 32-8
      const rect = el.getBoundingClientRect()
      const transY = window.innerHeight - rect.top - 32 - 8
      const transX = rect.left - 22 - 8
      this.$nextTick(() => {

        Velocity(el, {
          opacity: 1,
          // left: endLeft,
          // top: endTop,
          // translateX: -transX,
          translateY: transY,
        }, {
          duration: 600,
          easing: [.33, -0.59, .83, .67],
        })
        Velocity(el, {
          opacity: 1,
          // left: endLeft,
          // top: endTop,
          translateX: -transX,
        }, {
          duration: 598,
          easing: 'linear',
          complete: done,
          queue: false,
        })
      })
    },
    ballAfterEnter(el) {
      if ( this.balls.length > 0 ) {
        this.balls.shift()
      }
    },
    toogleList() {
      console.log('toogleList')
      if (!this.totalCount) {
        return
      }
      Vue.set(this,'fold',!this.fold)
      // this.fold = !this.fold
    },
    clearSelectFoods() {
      this.$emit('clear-selectfoods')
    },
  },
  computed: {
    totalPrice() {
      let total = 0
      this.selectFoods.forEach((food) => {
        total += food.price * food.count
      })
      return total
    },
    totalCount() {
      let count = 0
      this.selectFoods.forEach((food) => {
        count += food.count
      })
      return count
    },
    payDesc() {
      if (this.totalPrice === 0) {
        return `￥${this.minPrice}元起送`
      } else if (this.totalPrice < this.minPrice) {
        const diff = this.minPrice - this.totalPrice
        return `还差￥${diff}元起送`
      }
      return '去结算'
    },
    payClass() {
      if (this.totalPrice < this.minPrice) {
        return 'not-enough'
      }
      return 'enough'
    },
    listShow() {
      if (!this.totalCount) {
        return false
      }

      if (this.fold) {
        if (!this.scroll) {
          this.$nextTick(() => {
            this.scroll = new BScroll(this.$refs.listcontent, {
              click: true,
            })
          })
        }else{
          this.$nextTick(() => {
            this.scroll.refresh()
          })
        }
      }
      return this.fold
    },
  },
}
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixin"

  .shopcart
    position: fixed
    left: 0
    bottom: 0
    z-index: 50
    width: 100%
    height: 48px
    .content
      display: flex
      background: #141d27
      .content-left
        flex: 1
        .logo-wrapper
          display: inline-block
          vertical-align: top
          position: relative
          top: -10px
          margin: 0 12px
          padding: 6px
          width: 56px
          height: 56px
          box-sizing: border-box
          border-radius: 50%
          background: #141d27
          .logo
            width: 100%
            height: 100%
            border-radius: 50%
            background: #2b343c
            text-align: center
            .icon-shopping_cart
              line-height: 44px
              font-size: 24px
              color: #80858a
              &.highlight
                color: #fff
            &.highlight
              background: rgb(0, 160, 220)
          .num
            position: absolute
            top: 0px
            right: 0px
            width: 24px
            height: 16px
            font-size: 12px
            line-height: 16px
            text-align: center
            border-radius:16px
            font-weight: 700
            color: #fff
            background: rgb(240,20,20)
            box-shadow: 0 4px 8px 0 rgba(0,0,0,0.4)
        .price
          display: inline-block
          vertical-align: top
          margin-top: 12px
          line-height: 24px
          padding-right: 12px
          box-sizing: border-box
          border-right: 1px solid rgba(255,255,255,0.1)
          color: rgba(255,255,255,0.4)
          &.highlight
            color: #fff
        .desc
          display: inline-block
          vertical-align: top
          margin: 12px 0 0 12px
          line-height: 24px
          font-size: 10px
          color: rgba(255,255,255,0.4)
      .content-right
        flex: 0 0 105px
        width: 105px
        .pay
          height: 48px
          line-height: 48px
          text-align: center
          font-size: 12px
          font-weight: 700
          color: rgba(255,255,255,0.4)
          background: #2b333b
          &.not-enough
            background: #2b333b
          &.enough
            background: #00b43c
            color: #fff
    .ball-container
      .ball
        position: fixed
        left: 32px
        bottom: 22px
        z-index: 200
        width: 16px
        height: 16px
        border-radius: 50%
        background: rgb(0, 160, 220)
    .shopcart-list
      position: absolute
      left: 0px
      bottom: 48px
      width: 100%
      z-index: -1
      .list-header
        line-height: 40px
        height: 40px
        padding: 0px 8px
        background: #f3f5f7
        border-bottom: 1px solid rgba(7, 17, 27, 0.1)
        .title
          float: left
          font-size: 14px
          color: rgb(7, 17, 27)
        .empty
          display:inline-block
          margin: 0 10px
          float: right
          font-size: 12px
          color: rgb(0, 160, 20)
      .list-content
        padding: 0 18px
        max-height: 217px
        overflow: hidden
        background: #fff
        .food
          position: relative
          padding: 12px 0
          box-sizing: border-box
          border-1px(rgba(7, 17, 27, 0.1))
          .name
            line-height: 24px
            font-szie: 14px
            color: rgb(7, 17, 27)
          .price
            position: absolute
            right: 90px
            bottom: 12px
            line-height: 24px
            font-size: 14px
            font-weight: 700
            color: rgb(240, 20, 20)
          .cartcontrol-wrapper
            position: absolute
            right: 0px
            bottom: 6px

</style>
